import { Custom, CustomEnum, TableColumn } from 'custom/index'
import { TableColumnOption } from 'custom/custom/TableColumn'
import { Component } from 'vue'

interface EnumOptions {
  value: string | number | boolean;
  label: string | number;
}
export default class Table<T extends TableColumn = TableColumn> extends Custom {
  public static name: CustomEnum = 'Table';

  // 添加标准列 用来继承的时候被覆盖
  useColumn(prop: string | TableColumnOption = {}): T {
    const option: TableColumnOption = typeof prop === 'string' ? { prop } : prop
    const column = new TableColumn(option.prop)
    Object.assign(column._props, { minWidth: 100 }, option)
    this.addChild(column)
    return column
  }
  addColumn(prop: string | TableColumnOption = {}) {
    this.useColumn(prop)
    return this
  }

  // 添加index列
  useIndexColumn(option: TableColumnOption = {}) {
    const op = Object.assign({ prop: '_index', width: 50, type: 'index', label: '#', align: 'center', fixed: 'left' }, option)
    const column = this.useColumn(op)
    Object.assign(column._props, op)
    this.addChild(column)
    return column
  }
  addIndexColumn(option: Partial<TableColumnOption> = {}) {
    this.useIndexColumn(option)
    return this
  }

  // 添加选项
  useSelectionColumn(option: TableColumnOption = {}) {
    const op = Object.assign({ prop: '_selection', width: 50, type: 'selection', align: 'center', fixed: 'left' }, option)
    const column = this.useColumn(op)
    Object.assign(column._props, op)
    this.addChild(column)
    return column
  }
  addSelectionColumn(option: TableColumnOption = {}) {
    this.useSelectionColumn(option)
    return this
  }

  // 添加自定义列
  useCustomColumn(component: Component, prop: string | TableColumnOption = {}) {
    const option: TableColumnOption = typeof prop === 'string' ? { prop } : prop
    const column = this.useColumn(option)
    column.setComponent(component)
    return column
  }
  addCustomColumn(component: Component, prop: string | TableColumnOption = {}) {
    this.useCustomColumn(component, prop)
    return this
  }

  // 添加枚举列
  useEnumColumn(prop: string | TableColumnOption = {}, enumOptions: EnumOptions[] = []) {
    const option: TableColumnOption = typeof prop === 'string' ? { prop } : prop
    return this.useColumn(Object.assign({ formatter: (_, __, cellValue) => enumOptions.find(i => i.value === cellValue)?.label }, option))
  }
  addEnumColumn(prop: string | TableColumnOption = {}, enumOptions: EnumOptions[] = []) {
    this.useEnumColumn(prop, enumOptions)
    return this
  }
}
